package com.qianniu.napi.admin.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.base.Strings;
import com.qianniu.napi.admin.entity.Consult;
import com.qianniu.napi.admin.service.IConsultService;
import com.qianniu.napi.common.util.ObjectUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.Date;

/**
 * <p>
 * 商家咨询
 * </p>
 *
 * @author chenxuebin
 * @since 2018-10-16
 */
@Controller
public class ConsultController {

    private static final Logger logger = LoggerFactory.getLogger(ConsultController.class);

    @Autowired
    private IConsultService consultService;

    @RequestMapping("admin/consult/index")
    public String index() {
        return "admin/consult";
    }

    @RequestMapping("/admin/consult/list")
    @ResponseBody
    public Page<Consult> list(@RequestParam(value = "offset", required = false) Integer offset,
                              @RequestParam(value = "status", required = false) Integer status,
                              @RequestParam(value = "search", required = false) String search,
                              @RequestParam(value = "limit", required = false) Integer limit) {
        Wrapper<Consult> wrapper = new EntityWrapper<Consult>();
        int utype = ObjectUtil.getUserType();
        if (utype < 39)
            wrapper.eq("mpid", ObjectUtil.getMpid());
        else if (utype < 99)
            wrapper.eq("mid", ObjectUtil.getMid());
        if (!Strings.isNullOrEmpty(search))
            wrapper.like("name", search).or().like("phone", search);

        wrapper.orderBy("id", false);
        Page<Consult> page = consultService.selectPage(new Page<Consult>(offset / limit + 1, limit), wrapper);
        return page;
    }

    @RequestMapping("/admin/consult/add")
    public String add(@Valid Consult bean, BindingResult bindingResult) {
        // 字段校验
        if (bindingResult.hasErrors()) {
            logger.debug("There are errors! {}", bindingResult);
            return null;
        }
        bean.setCreatetime(new Date());
        bean.setMid(ObjectUtil.getMid());
        bean.setMpid(ObjectUtil.getMpid());
        bean.setUid(ObjectUtil.getUid());
        bean.insert();
        return "admin/consult";
    }

    @RequestMapping("/admin/consult/getConsultById")
    @ResponseBody
    public Consult getConsultById(long id) {
        Consult consult = consultService.selectById(id);
        return consult;
    }

    @RequestMapping("/admin/consult/deleteOne")
    @ResponseBody
    public int deleteOne(long id) {
        boolean isSucc = consultService.deleteById(id);
        return isSucc ? 1 : 0;
    }

    @RequestMapping("/admin/consult/modify")
    public String update(Consult bean) {
//        bean.setModifytime(new Date());
//        bean.setModifier(ObjectUtil.getUid());
        boolean isSuccc = consultService.updateById(bean);
        if (isSuccc) {
            return "redirect:index";
        }
        return "error";
    }

}
